package top.frsf.mqttproviderapp.callback;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
public class MqttProviderCallback implements MqttCallback {

    @Value("${mqtt.client.id}")
    private String clientId;

    @Override
    public void connectionLost(Throwable throwable) {
        //与服务器断开的回调
        log.info(clientId + "与服务器断开连接");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        //消息到达的回调
        log.info("Message Arrived.");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        //消息发布成功的回调
        IMqttAsyncClient client = iMqttDeliveryToken.getClient();
        log.info(client.getClientId() + "发布消息成功！");
    }
}
